package com.qqtech.qquan.delivery.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.delivery.model.DeliveryOrder;
import com.qqtech.qquan.delivery.model.DeliveryOrderQuery;
import com.qqtech.qquan.delivery.model.DeliveryOrderVo;
import com.qqtech.qquan.delivery.service.DeliveryOrderService;
import com.qqtech.qquan.user.model.UserLoginVo;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/delivery/order")
public class DeliveryOrderController extends BaseWebController<DeliveryOrder, DeliveryOrderQuery> {
	@Resource
	private DeliveryOrderService deliveryOrderService;
    
	@Override
	protected BaseService<DeliveryOrder> getBaseService() {
		return deliveryOrderService;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody DeliveryOrderVo deliveryOrderVo) {
		Result result = new Result();
		if (deliveryOrderVo == null) {
			result.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(deliveryOrderVo.getDeliveryName())) {
				result.addErr("物流公司不能留空");
			}
			if (StringUtil.isBlank(deliveryOrderVo.getDeliveryNo())) {
				result.addErr("物流号不能留空");
			}
			if (deliveryOrderVo.getAddressId() == null) {
				result.addErr("代送的地址不能为空");
			}
		}
		if (result.checkForm()) {
			UserLoginVo userLoginVo = super.getLoginUser();
			Integer userId = userLoginVo.getId();
			deliveryOrderVo.setCreatorId(userId);
			result = deliveryOrderService.handleSaveOrder(deliveryOrderVo);
		}
		return result;
	}
}
